﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.IO;

namespace XMM
{
    public class Tools
    {
        /// <summary>
        /// Check if all req. directories in media-library-path exist.
        /// </summary>
        public static void checkMediaLibraryPath()
        {
            Settings oSettings = new Settings();

            try
            {
                if (Directory.Exists(oSettings.LibraryDataStorage) == false)
                {
                    if (MessageBox.Show("Das Verzeichnis \"" + oSettings.LibraryDataStorage + "\" wurde nicht gefunden! Möchten Sie jetzt in diesem Ordner die Verzeichnisse für die Medienbibliothek anlegen?", "Medienbibliothek nicht gefunden", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.No)
                        return;

                    Directory.CreateDirectory(oSettings.LibraryDataStorage);
                }

                if (Directory.Exists(oSettings.LibraryDataStorage + "temp") == false)
                    Directory.CreateDirectory(oSettings.LibraryDataStorage + "temp");

                if (Directory.Exists(oSettings.LibraryDataStorage + "movies") == false)
                    Directory.CreateDirectory(oSettings.LibraryDataStorage + "movies");

                if (Directory.Exists(oSettings.LibraryDataStorage + "movies\\images") == false)
                    Directory.CreateDirectory(oSettings.LibraryDataStorage + "movies\\images");

                if (Directory.Exists(oSettings.LibraryDataStorage + "movies\\thumbs") == false)
                    Directory.CreateDirectory(oSettings.LibraryDataStorage + "\\movies\\thumb");

                if (Directory.Exists(oSettings.LibraryDataStorage + "movies\\discs") == false)
                    Directory.CreateDirectory(oSettings.LibraryDataStorage + "movies\\discs");

                if (Directory.Exists(oSettings.LibraryDataStorage + "series") == false)
                    Directory.CreateDirectory(oSettings.LibraryDataStorage + "series");

                if (Directory.Exists(oSettings.LibraryDataStorage + "series\\images") == false)
                    Directory.CreateDirectory(oSettings.LibraryDataStorage + "series\\images");

                if (Directory.Exists(oSettings.LibraryDataStorage + "series\\thumbs") == false)
                    Directory.CreateDirectory(oSettings.LibraryDataStorage + "series\\thumb");

                if (Directory.Exists(oSettings.LibraryDataStorage + "series\\discs") == false)
                    Directory.CreateDirectory(oSettings.LibraryDataStorage + "series\\discs");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fehler beim Überprüfen/Anlegen der Medienbibliothek auf der Festplatte ist ein Fehler aufgetreten: " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        public static decimal ConvertBytesToMegabytes(Int64 iTotalBytes)
        {
            float fMegabytes = (float)iTotalBytes / 1024 / 1024;
            decimal dMegabytes = (decimal)fMegabytes;
            dMegabytes = Math.Round(dMegabytes, 2);

            return dMegabytes;
        }

        /// <summary>
        /// Check if Text in ComboBox matches an Item in its List.
        /// </summary>
        /// <param name="cmbTarget">(ComboBox) ComboBox to validate.</param>
        /// <returns>[true] if entered text matches any item.
        /// [false] if Text was not found.</returns>
        public static bool validateComboBox(ComboBox cmbTarget)
        {
            if (cmbTarget.Items != null && cmbTarget.Items.Count > 0)
            {
                foreach (Object oTargetItem in cmbTarget.Items)
                {
                    if (oTargetItem.ToString() == cmbTarget.Text)
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Get the Installation-Path of DIG-ITMan.
        /// </summary>
        /// <returns>(string) Path to Installation (e.g.: C:\Programme\DIG-ITMan\)</returns>
        public static string getInstallationPath()
        {
            string InstallationPath = Environment.CurrentDirectory + "\\";

            return InstallationPath;
        }

        public static string convertDatabaseTime(DateTime dtSelectedTime)
        {
            string sDatabaseTimeString = String.Empty;

            if (dtSelectedTime != null)
            {
                sDatabaseTimeString = dtSelectedTime.Year.ToString() + "-" + dtSelectedTime.Month.ToString() + "-" + dtSelectedTime.Day.ToString();
            }

            return sDatabaseTimeString;
        }

        /// <summary>
        /// Replace SQL-Query-breaking characters.
        /// </summary>
        /// <param name="sInputString"></param>
        /// <returns></returns>
        public static string getCleanString(string sInputString)
        {
            string sCleanString = sInputString;
            string sFullCleanString = String.Empty;

            // Replace Characters
            foreach (char cCurrentInputCharacter in sCleanString)
            {
                if (cCurrentInputCharacter == '\\') // Correctly Escape \-Char for SQL-Statement
                {
                    sFullCleanString += "\\\\";
                }
                else if (cCurrentInputCharacter == '"') // Replace all occurances of "
                {
                    sFullCleanString += "'";
                }
                else
                    sFullCleanString += cCurrentInputCharacter;
            }


            return sFullCleanString;
        }

        /// <summary>
        /// Get File-Name from File-Path.
        /// </summary>
        /// <param name="sFullPath">(string) Full path to target-file.</param>
        /// <returns>(string) Filename with extension.</returns>
        public static string getFileName(string sFullPath)
        {
            string sFileName = String.Empty;

            try
            {
                if (sFullPath != String.Empty)
                {
                    while (sFullPath.Contains(';'))
                    {
                        sFullPath = sFullPath.Remove(sFullPath.IndexOf(';'), 1);
                    }
                    sFileName = sFullPath.Substring((sFullPath.LastIndexOf('\\') + 1), ((sFullPath.Length - sFullPath.LastIndexOf('\\')) - 1));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fehler beim Extrahieren des Datei-Namens aus dem Pfad \"" + sFullPath + "\": " + ex.Message + Environment.NewLine + Environment.NewLine + "Bitte überprüfen Sie den angegebenen Pfad!", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            return sFileName;
        }

        public static string getMediaTypeName(int iMediaType)
        {
            string sMediaType = "Unbekannt";

            switch (iMediaType)
            {
                case 0:
                    sMediaType = "Unbekannt";
                    break;
                case 1:
                    sMediaType = "Film";
                    break;
                case 2:
                    sMediaType = "Episode";
                    break;
                case 3:
                    sMediaType = "Musik-Titel";
                    break;
                case 4:
                    sMediaType = "Bild";
                    break;
            }

            return sMediaType;
        }

        /// <summary>
        /// Get new Rotation-Mode from direction and current rotation.
        /// </summary>
        /// <param name="oCurrentRotation">(System.Windows.Media.Imaging.Rotation) Current Rotation-Mode.</param>
        /// <param name="bClockwise">(bool) [true] for clockwise, [false] for counter-clockwise rotation.</param>
        /// <returns></returns>
        /// <remarks>Cycles rotation by 90° (counter-)clockwise.</remarks>
        public static System.Windows.Media.Imaging.Rotation getRotationNewMode(System.Windows.Media.Imaging.Rotation oCurrentRotation, bool bClockwise)
        {
            System.Windows.Media.Imaging.Rotation oNewRotation = oCurrentRotation;

            if (bClockwise)
            {
                switch (oCurrentRotation)
                {
                    case System.Windows.Media.Imaging.Rotation.Rotate0:
                        oNewRotation = System.Windows.Media.Imaging.Rotation.Rotate90;
                        break;
                    case System.Windows.Media.Imaging.Rotation.Rotate90:
                        oNewRotation = System.Windows.Media.Imaging.Rotation.Rotate180;
                        break;
                    case System.Windows.Media.Imaging.Rotation.Rotate180:
                        oNewRotation = System.Windows.Media.Imaging.Rotation.Rotate270;
                        break;
                    case System.Windows.Media.Imaging.Rotation.Rotate270:
                        oNewRotation = System.Windows.Media.Imaging.Rotation.Rotate0;
                        break;
                }
            }
            else
            {
                switch (oCurrentRotation)
                {
                    case System.Windows.Media.Imaging.Rotation.Rotate0:
                        oNewRotation = System.Windows.Media.Imaging.Rotation.Rotate270;
                        break;
                    case System.Windows.Media.Imaging.Rotation.Rotate90:
                        oNewRotation = System.Windows.Media.Imaging.Rotation.Rotate0;
                        break;
                    case System.Windows.Media.Imaging.Rotation.Rotate180:
                        oNewRotation = System.Windows.Media.Imaging.Rotation.Rotate90;
                        break;
                    case System.Windows.Media.Imaging.Rotation.Rotate270:
                        oNewRotation = System.Windows.Media.Imaging.Rotation.Rotate180;
                        break;
                }
            }

            return oNewRotation;
        }

        /// <summary>
        /// Delete all files in [LIBPATH]\temp\.
        /// </summary>
        public static void clearTemp()
        {
            Settings oSettings = new Settings();

            if (Directory.Exists(oSettings.LibraryDataStorage + "temp"))
            {
                string[] dTempDirFiles = Directory.GetFiles(oSettings.LibraryDataStorage + "temp", "*.jpg", SearchOption.TopDirectoryOnly);

                if (dTempDirFiles != null && dTempDirFiles.Count() > 0)
                {
                    foreach(string sTempFile in dTempDirFiles)
                    {
                        try
                        {
                            File.Delete(sTempFile);
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
            }
        }

        public static string getVersion()
        {
            return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }
    }
}
